package dtaskboard.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;

import dtaskboard.client.login.LoginPage;
import dtaskboard.client.main.MainPage;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Dtaskboard implements EntryPoint {

  // private final GreetingServiceAsync greetingService = GWT
  // .create(GreetingService.class);

  /**
   * This is the entry point method.
   */
  @Override
  public void onModuleLoad() {

    LoginServiceAsync loginService = GWT.create(LoginService.class);
    String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
    loginService.isAuthenticated(sessionId, new AsyncCallback<Boolean>() {

      @Override
      public void onSuccess(Boolean result) {
        if (result == null || !result) {
          new LoginPage();
        } else {
          new MainPage();
        }
      }

      @Override
      public void onFailure(Throwable caught) {
        new LoginPage();

      }
    });
  }
}
